/*
 * @Descripttion: 
 * @version: 
 * @Author: cyj
 * @Date: 2022-12-05 21:24:40
 */

//对axios进行二次封装
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { getItem } from '@/utils/utils.js'
//引入加载效果
import { startLoading, endLoading } from './loading'


//创建实例`
const request = axios.create({
    //服务器基本URL
    baseURL: "http://localhost:3000",
    timeout: 10000,
    //运行跨域 携带cookie
    withCredentials: true
})

//请求拦截器
request.interceptors.request.use(
    //config配置项，包含header请求头
    config => {
        if (config.url != '/login/qr/check') {     //排除检查二维码状态
            //没有得到服务器的数据前展示loading效果
            startLoading()
        }
        // if (getItem('cookie')) {  //如果存储中存在cookie
        //     const cookie = getItem('cookie')  //获取cookie
        //     // config.headers.Authorization = cookie
        // }
        return config
    },
    error => {
        return Promise.reject(error)
    }
) 


//响应拦截器
request.interceptors.response.use(
    response => {
        //得到数据之后，结束loading效果
        endLoading()
        return response
    },
    error => {
        ElMessage({
            showClose: true,
            message: error.message,
            center: true
        })
    }
)

request.defaults.withCredentials = true

export default request